package com.rimi.controller;

import com.rimi.bean.Comments;
import com.rimi.bean.User;
import com.rimi.commons.CommonsDataResult;
import com.rimi.commons.ResultStaus;
import com.rimi.service.CommentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @
 * @author:liangLi
 * @time:2020/7/7 16:11
 */
@CrossOrigin
@Controller
@Api(value = "发布信息" ,description = "发布信息")

public class CommentsController {
    @Autowired
    CommentsService commentsService;

    @PostMapping("/comment")
    @ResponseBody
    @ApiOperation("发布信息")

    public String addCommentSInfo(Comments comments, HttpSession httpSession){

        User user = (User) httpSession.getAttribute("user");

        comments.setUserid(user.getUserId());

        Integer integer = commentsService.addCommentSInfo(comments);

        if(integer==1){
            return "发布成功";
        }else {
            return "发布失败";
        }
    }

    @GetMapping("/show")
    @ResponseBody
    @ApiOperation("显示发布信息")

    public CommonsDataResult queryAllCommentsInfo(){


        List<Comments> commentSInfo = commentsService.getCommentSInfo();

        List<Object> list=new ArrayList<>();
        for (Comments comments : commentSInfo) {
            list.add(comments.getRecruit_time());
            list.add(comments.getRecruit_need());
            list.add(comments.getRecruit_price());
            list.add(comments.getRecruit_describe());
            list.add(comments.getRecruit_phone());
            list.add(comments.getRecruit_number());
            list.add(comments.getUserid());
            list.add(comments.getNumber());
        }
        if (list.size()==0){
            return new CommonsDataResult(ResultStaus.EXCEPTION_ERROR,list);
        }else {
            return new CommonsDataResult(ResultStaus.OK,list);
        }

    }
}
